/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest.front;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.bo.req.*;
import me.zhengjie.modules.system.bo.resp.QztyIndexResp;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.service.*;
import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.SmsAliUtils;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.bo.MllSms;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @website https://eladmin.vip
* @author cjl
* @date 2023-05-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "DCC趣味科普相关管理")
@RequestMapping("/front/qwkp")
public class FrontQuWeiKePuController {



    private final DccXiaoxiService dccXiaoxiService;

    private final RedisUtils redisUtils;
    private final DccPinglunService dccPinglunService;
    private final DccDianzanService dccDianzanService;
    private final DccCollectService dccCollectService;
    private final DccKpspService dccKpspService;


    @PostMapping("/index")
    @AnonymousAccess
    @ApiOperation(value = "趣味视频", httpMethod = "POST")
    public ResponseEntity<Object> index(@RequestBody QueryBaseReq baseReq) {
        DccKpspQueryCriteria criteria =new DccKpspQueryCriteria();
        Sort sort =Sort.by(Sort.Direction.ASC,"num");
        Pageable pageable =PageRequest.of(baseReq.getPage()-1, baseReq.getSize(),sort);
        Map<String, Object> stringObjectMap = dccKpspService.queryAllFront(criteria, pageable);
        return new ResponseEntity<>(stringObjectMap,HttpStatus.OK);
    }



    @GetMapping("/getId")
    @AnonymousAccess
    @ApiOperation(value = "趣味视频", httpMethod = "GET")
    public ResponseEntity<Object> getId(@RequestParam Integer id) {
        DccKpspDto byId = dccKpspService.findById(id);
        return new ResponseEntity<>(byId,HttpStatus.OK);
    }


    @Log("评论")
    @ApiOperation("评论")
    @AnonymousAccess
    @PostMapping(value = "/pinglun")
    public ResponseEntity<Object> pinglun(@RequestBody DccPingLunJReq dccPingLunJReq){
        if (!StpUtil.isLogin()){
            return new ResponseEntity<>(null,HttpStatus.UNAUTHORIZED);
        }

        //判断类型是1还是2
        DccPinglun resources=new DccPinglun();
        DccUserDto user = (DccUserDto) StpUtil.getSession().get("user");

        if (dccPingLunJReq.getType()==1){
            resources.setUid(StpUtil.getLoginIdAsString());
            resources.setComment(dccPingLunJReq.getContent());
            resources.setCommonId(dccPingLunJReq.getId());
            resources.setCreatedAt(new Timestamp(System.currentTimeMillis()));
            resources.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
            resources.setOperator("视频评论");
            dccPinglunService.create(resources);
            return new ResponseEntity<>("评论成功",HttpStatus.OK);

        }else if (dccPingLunJReq.getType()==2){
            resources.setUid(StpUtil.getLoginIdAsString());
            resources.setComment(dccPingLunJReq.getContent());
            resources.setCommonId(dccPingLunJReq.getId());
            resources.setCreatedAt(new Timestamp(System.currentTimeMillis()));
            resources.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
            resources.setOperator("评论关联评论"+dccPingLunJReq.getPinglunId());
            DccPinglunDto dccPinglunDto=dccPinglunService.create(resources);
            //评论的时候推送消息
            DccXiaoxi dccXiaoxi=new DccXiaoxi();
            //根据评论编号查出来uid
            DccPinglunDto byId = dccPinglunService.findById(dccPingLunJReq.getPinglunId());
            dccXiaoxi.setUid(byId.getUid());
            dccXiaoxi.setStatus(1);
            dccXiaoxi.setCommonId(dccPinglunDto.getId());
            dccXiaoxi.setType(1);
            dccXiaoxi.setTitle("评论回复");
            dccXiaoxi.setText(StringUtils.isAllEmpty(user.getNickName())?"路人":user.getNickName()+"回复了您的评论");
            dccXiaoxi.setCreatedAt(new Timestamp(System.currentTimeMillis()));
            dccXiaoxi.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
            dccXiaoxiService.create(dccXiaoxi);
            return new ResponseEntity<>("评论成功",HttpStatus.OK);
        }else{
            return new ResponseEntity<>("评论类型不存在",HttpStatus.OK);
        }

    }

    @Log("点赞")
    @ApiOperation("点赞")
    @AnonymousAccess
    @PostMapping(value = "/dianzan")
    public ResponseEntity<Object> dianzan(@RequestBody DccDianzanJReq dccPingLunJReq){
        if (!StpUtil.isLogin()){
            return new ResponseEntity<>(null,HttpStatus.UNAUTHORIZED);
        }

        DccUserDto user = (DccUserDto) StpUtil.getSession().get("user");
        DccDianzan resources=new DccDianzan();
        resources.setUid(StpUtil.getLoginIdAsString());
        resources.setCommonId(dccPingLunJReq.getId());
        resources.setStatus(1);
        resources.setCreatedAt(new Timestamp(System.currentTimeMillis()));
        resources.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
        DccDianzanDto dccDianzanDto=dccDianzanService.create(resources);
        //评论的时候推送消息
        DccXiaoxi dccXiaoxi=new DccXiaoxi();
        //根据评论编号查出来uid
        DccPinglunDto byId = dccPinglunService.findById(dccPingLunJReq.getId());
        dccXiaoxi.setUid(byId.getUid());
        dccXiaoxi.setStatus(1);
        dccXiaoxi.setCommonId(dccDianzanDto.getId());
        dccXiaoxi.setType(2);
        dccXiaoxi.setTitle("评论点赞");
        dccXiaoxi.setText(StringUtils.isAllEmpty(user.getNickName())?"路人":user.getNickName()+"点赞了您的评论");
        dccXiaoxi.setCreatedAt(new Timestamp(System.currentTimeMillis()));
        dccXiaoxi.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
        dccXiaoxiService.create(dccXiaoxi);
        return new ResponseEntity<>("点赞成功",HttpStatus.OK);
    }

    @Log("收藏")
    @ApiOperation("收藏")
    @AnonymousAccess
    @PostMapping(value = "/shoucang")
    public ResponseEntity<Object> shoucang(@RequestBody DccShoucangJReq dccShoucangJReq){
        if (!StpUtil.isLogin()){
            return new ResponseEntity<>(null,HttpStatus.UNAUTHORIZED);
        }

        DccCollect resources=new DccCollect();
        resources.setUid(StpUtil.getLoginIdAsString());
        resources.setCommonId(dccShoucangJReq.getId());
        resources.setStatus(1);
        resources.setCreatedAt(new Timestamp(System.currentTimeMillis()));
        resources.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
        resources.setTitle(dccShoucangJReq.getTitle());
        resources.setImage(dccShoucangJReq.getImage());
        resources.setContent(dccShoucangJReq.getContent());
        dccCollectService.create(resources);
        return new ResponseEntity<>("收藏成功",HttpStatus.OK);
    }


    @Log("科普问卷列表")
    @ApiOperation("科普问卷列表")
    @AnonymousAccess
    @GetMapping(value = "/question/list")
    public ResponseEntity<Object> list() {
        JSONObject jsonObject=new JSONObject();
        List<JSONObject> list=new ArrayList<>();

        JSONObject jsonObject1=new JSONObject();
        jsonObject1.put("question","在这个视频里面提到了哪些新型毒品？");
        jsonObject1.put("answer","A病毒,B海洛因");
        jsonObject1.put("correct","A");
        jsonObject1.put("number","1");
        jsonObject1.put("id","1");
        JSONObject jsonObject2=new JSONObject();
        jsonObject2.put("question","在这个视频里面提到了哪些旧型毒品？");
        jsonObject2.put("answer","A海洛因,B病毒");
        jsonObject2.put("correct","B");
        jsonObject2.put("number","2");
        jsonObject2.put("id","2");
        JSONObject jsonObject3=new JSONObject();
        jsonObject3.put("question","在这个视频里面提到了哪些O型毒品？");
        jsonObject3.put("answer","A海洛因,B病毒");
        jsonObject3.put("correct","B");
        jsonObject3.put("number","3");
        jsonObject3.put("id","3");
        JSONObject jsonObject4=new JSONObject();
        jsonObject4.put("question","在这个视频里面提到了哪些1型毒品？");
        jsonObject4.put("answer","A海洛因,B病毒");
        jsonObject4.put("correct","B");
        jsonObject4.put("number","4");
        jsonObject4.put("id","4");
        JSONObject jsonObject5=new JSONObject();
        jsonObject5.put("question","在这个视频里面提到了哪些2型毒品？");
        jsonObject5.put("answer","A海洛因,B病毒");
        jsonObject5.put("correct","B");
        jsonObject5.put("number","5");
        jsonObject5.put("id","5");
        list.add(jsonObject1);
        list.add(jsonObject2);
        list.add(jsonObject3);
        list.add(jsonObject4);
        list.add(jsonObject5);
        jsonObject.put("question",list);

        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }

    @Log("问卷答题")
    @ApiOperation("问卷答题")
    @PostMapping(value = "/submit")
    @AnonymousAccess
    public ResponseEntity<Object> submit(@Validated @RequestBody DccDTWJReq dccDTWJReq) throws Exception {
        if (!StpUtil.isLogin()){
            return new ResponseEntity<>(null,HttpStatus.UNAUTHORIZED);
        }

        //todo 目前没积分，这个接口先返回为空
            redisUtils.set("submit"+StpUtil.getLoginIdAsString(),JSON.toJSONString(dccDTWJReq));

            return new ResponseEntity<>("提交成功",HttpStatus.OK);
    }


}